package com.eshop.web.controllers.api;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.eshop.common.util.security.MD5;
import com.eshop.frameworks.core.controller.BaseController;
import com.eshop.model.mongodb.EUser;
import com.eshop.service.mongodb.EUserService;

@Controller
@RequestMapping("/api")
public class UserApiController extends BaseController {
	private static final Logger logger = Logger.getLogger(UserApiController.class);
	private static final Integer ERR_PARAM=-1;
	private static final Integer ERR_CODE=-2;
	private static final Integer ERR_EXIST=-3;
	private static final Integer ERR_MOBILE=-4;
	@Autowired
	private EUserService euserService;
	
	@ResponseBody
	@RequestMapping(value = "/regist", method = RequestMethod.POST)
	public String regist(EUser euser, String key,String code, HttpServletRequest request,
			HttpServletResponse response) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("success", true);
		try {
			
			if(euser==null){
				jsonObject.put("success", false);
				jsonObject.put("msg", "参数错误");
				jsonObject.put("code", ERR_PARAM);
				return jsonObject.toJSONString();
			}
			String mobile = euser.getMobile();
			EUser user = euserService.getByMobile(mobile);
			if(user!=null){
				jsonObject.put("success", false);
				jsonObject.put("msg", "手机号已经注册");
				jsonObject.put("code", ERR_EXIST);
				return jsonObject.toJSONString();
			}
			if (mobile != null&&key!=null&&key.equals(MD5.getMD5(mobile))) {
				String cd = (String) this.getSessionAttribute(request, euser.getMobile() + "_code");
				if(code!=null&&code.equals(cd)){
					euser.setPassword(MD5.getMD5(euser.getPassword()));
					euser.setRegTime(new Date());
					euser.setIsMobileVerified(1);
					euser.setSource("phone");
					euser.setUsername(mobile);
					euserService.save(euser);
					euser =euserService.getByMobile(euser);
					jsonObject.put("userId", euser.getId());
				}else{
					jsonObject.put("success", false);
					jsonObject.put("code", ERR_CODE);
					jsonObject.put("msg", "验证码错误");
				}
			}else{
				jsonObject.put("success", false);
				jsonObject.put("code", ERR_MOBILE);
				jsonObject.put("msg", "手机号错误");
			}
		} catch (Exception e) {
			logger.error("UserApiController.regist", e);
			jsonObject.put("success", false);
		}
		return jsonObject.toJSONString();
	}

	@ResponseBody
	@RequestMapping(value = "/doLogin", method = RequestMethod.POST)
	public String doLogin(EUser euser, HttpServletRequest request,HttpServletResponse response) {
		JSONObject jsonObject = new JSONObject();
		EUser user = euserService.getByMobileAndPwd(euser);
		if(user!=null){
			jsonObject.put("success", true);
			jsonObject.put("userId", user.getId());
		}else{
			jsonObject.put("success", false);
			jsonObject.put("msg", "手机号或密码错误");
		}
		return jsonObject.toJSONString();
	}
}
